package com.peanut.baby.mvp.launcher;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;

import androidx.annotation.NonNull;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.InitialAd;
import com.tencent.mars.xlog.Log;
import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/6/15.
 */

public class LauncherPresenter implements LauncherContract.Presenter{

    private LauncherContract.View   mView;
    private Activity activity;
    public LauncherPresenter(LauncherContract.View view,Activity activity){
        this.mView = view;
        this.activity = activity;
    }
    @Override
    public void getAd() {
        Observable<BaseResponse<InitialAd>> adObservable = RetrofitClient.getInstance()
                .getInitialAd(InitManager.getInstance().getUserId());

        adObservable.compose(BaseScheduler.<BaseResponse<InitialAd>>compose())
                .compose(RxLifecycle.bind(activity).withObservable()).subscribe(new BaseObserver<InitialAd>() {
            @Override
            protected void onHandleSuccess(InitialAd o) {
                mView.onGetAdSuccess(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                mView.onGetAdError(msg);
            }

        });


    }

    @Override
    public void checkPermission(final Activity ctx) {

        AndPermission.with(ctx)
                .requestCode(100)
                .permission(Manifest.permission.READ_PHONE_STATE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .rationale(new RationaleListener(){

                    @Override
                    public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
                        AlertDialog.newBuilder(ctx)
                                .setTitle("友好提醒")
                                .setMessage("为了应用正常运行，请授权应用所请求的权限")
                                .setPositiveButton("好，给你", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.resume();
                                    }
                                })
                                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.cancel();
                                    }
                                })
                                .show();
                    }
                })
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        Log.d("LauncherActivity","PermissionListener.SUCCESS");
                        mView.onPermissionGranted(grantPermissions);
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        Log.d("LauncherActivity","PermissionListener.FAIL");
                        mView.onPermissionDenied(deniedPermissions);
                    }
                })
                .start();
    }

}
